﻿using RpcSync.Model;
using RpcSync.Model.DB;
using SqlSugar;
using SqlSugarHelper;

namespace RpcSync.DAL.Repository
{
    internal class SysConfigDAL : ISysConfigDAL
    {
        private readonly IRepository<SysConfigModel> _BasicDAL;
        public SysConfigDAL(IRepository<SysConfigModel> dal)
        {
            _BasicDAL = dal;
        }

        public SysConfig[] GetSysConfig(string type)
        {
            string[] types = new string[] { type, string.Empty };
            return this._BasicDAL.Gets(c => types.Contains(c.SystemType) && c.IsEnable, a => new SysConfig
            {
                Name = a.Name,
                Prower = a.Prower,
                RpcMerId = a.RpcMerId,
                ServerId = a.ServerId,
                Value = a.Value,
                ValueType = a.ValueType,
                ToUpdateTime = a.ToUpdateTime,
            });
        }

        public Dictionary<string,DateTime> GetToUpdateTime()
        {
            return this._BasicDAL.GroupBy(a => a.SystemType, a => new
            {
                a.SystemType,
                MaxTime = SqlFunc.AggregateMax(a.ToUpdateTime)
            }).ToDictionary(c => c.SystemType, c => c.MaxTime);
        }
    }
}
